package com.cgwx.systemservice.controller;


import com.cgwx.commonutils.result.SysResult;
import com.cgwx.springsecurity.annotation.Log;
import com.cgwx.springsecurity.enums.BusinessType;
import com.cgwx.systemservice.entity.vo.AssginRoleVo;
import com.cgwx.systemservice.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户角色 前端控制器
 * </p>
 *
 * @author GaoWei
 * @since 2023-10-12
 */

@Api(tags = "用户角色前端控制器")
@RestController
@RequestMapping("/system/sysUserRole")
@CrossOrigin
public class SysUserRoleController {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @ApiOperation(value = "根据用户ID获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public SysResult toAssign(
            @ApiParam(name = "userId", value = "用户id")
            @PathVariable Long userId) {
        Map<String, Object> roleMap = sysUserRoleService.getRolesByUserId(userId);
        return SysResult.ok(roleMap);
    }

    @Log(title = "根据用户分配角色", businessType = BusinessType.ASSGIN)
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public SysResult doAssign(
            @ApiParam(name = "assginRoleVo", value = "分配角色Vo：用户id，分配的角色id列表")
            @RequestBody AssginRoleVo assginRoleVo) {
        sysUserRoleService.doAssign(assginRoleVo);
        return SysResult.ok();
    }
}

